package ca.ericslandry.client.mvp.view;

import net.customware.gwt.presenter.client.place.PlaceRequest;
import ca.ericslandry.client.mvp.presenter.IssueListPresenter;
import ca.ericslandry.client.mvp.presenter.IssuePresenter;
import ca.ericslandry.client.mvp.presenter.MainPresenter;
import ca.ericslandry.client.mvp.presenter.PeoplePresenter;
import ca.ericslandry.client.mvp.presenter.PlacesPresenter;
import ca.ericslandry.client.resource.MyConstants;
import ca.ericslandry.client.resource.MyImages;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.SplitLayoutPanel;
import com.google.gwt.user.client.ui.StackLayoutPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class MainView extends BaseView implements MainPresenter.Display {
	private SimplePanel header = new SimplePanel();
	private SimplePanel content = new SimplePanel();
	private SplitLayoutPanel split = new SplitLayoutPanel();
	private DockLayoutPanel panel = new DockLayoutPanel(Unit.PX);
	private static final int HEADER_HEIGHT = 50;

	public MainView() {
		panel.addNorth(header, 50);
		split.addWest(getNavigation(), 200);
		split.add(content);
		panel.add(split);
	}

	@Override
	public Widget asWidget() {
		return panel;
	}

	@Override
	public void setContent(Widget widget) {
		content.clear();
		content.add(widget);
	}

	@Override
	public void setHeader(Widget widget) {
		header.clear();
		header.add(widget);
	}

	private Widget getNavigation() {
		StackLayoutPanel p = new StackLayoutPanel(Unit.PX);
		p.add(getIssuesLinks(), getIssuesHeader(), HEADER_HEIGHT);
		p.add(getPeopleLinks(), getPeopleHeader(), HEADER_HEIGHT);
		p.add(getPlacesLinks(), getPlacesHeader(), HEADER_HEIGHT);
		p.add(getAboutLinks(), getAboutHeader(), HEADER_HEIGHT);
		return p;
	}

	private Widget getIssuesLinks() {
		VerticalPanel p = new VerticalPanel();
		p.add(IssueListPresenter.getLinkAllIssues());
		p.add(IssueListPresenter.getLinkIssuesByMe());
		p.add(IssuePresenter.getLinkCreateIssue());
		return p;
	}

	private Widget getPeopleLinks() {
		VerticalPanel p = new VerticalPanel();
		p.add(PeoplePresenter.getLinkPeopleAroundMe());
		p.add(PeoplePresenter.getLinkFindPeople());
		p.add(PeoplePresenter.getLinkAddPerson());
		p.add(PeoplePresenter.getLinkPeopleRating());
		p.add(PeoplePresenter.getLinkCivicUsers());
		return p;
	}

	private Widget getPlacesLinks() {
		VerticalPanel p = new VerticalPanel();
		p.add(PlacesPresenter.getLinkMyLocation());
		p.add(PlacesPresenter.getLinkBrowseLocations());
		return p;
	}

	private Widget getAboutLinks() {
		VerticalPanel p = new VerticalPanel();
		p.add(new HTML("<a href=\"http://code.google.com/p/sixfixmix/\">Project Home</a>"));
		return p;
	}

	private Widget getIssuesHeader() {
		return createStackHeader(MyConstants.INSTANCE.issues(), MyImages.INSTANCE.issuesgroup(), IssueListPresenter
				.getDefaultPlaceRequest());
	}

	private Widget getPeopleHeader() {
		return createStackHeader(MyConstants.INSTANCE.people(), MyImages.INSTANCE.contactsgroup(), PeoplePresenter
				.getDefaultPlaceRequest());
	}

	private Widget getPlacesHeader() {
		return createStackHeader(MyConstants.INSTANCE.places(), MyImages.INSTANCE.mapgroup(), PlacesPresenter
				.getDefaultPlaceRequest());
	}

	private Widget getAboutHeader() {
		// TODO: Fix target
		return createStackHeader(MyConstants.INSTANCE.about(), MyImages.INSTANCE.infogroup(), IssueListPresenter
				.getDefaultPlaceRequest());
	}

	private Widget createStackHeader(String text, ImageResource imageResource, PlaceRequest placeReq) {
		Image image = new Image(imageResource);
		String captionHTML = "<table class='caption' cellpadding='0' cellspacing='5'>"
			+ "<tr><td class='lcaption'><img src=\"" + image.getUrl()
			+ "\"/></td><td class='rcaption'><b style='white-space:nowrap'>" + text + "</b></td></tr></table>";
		return new Hyperlink(captionHTML, true, placeReq.toString());
	}
}